package com.sskj.linechat;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;

import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.linechat.activity.SignPlatformActivity;
import com.sskj.linechat.bean.LineBean;
import com.sskj.linechat.utils.NumberUtil;
import com.sskj.linechat.utils.UIUtil;
import com.sskj.linechat.widght.CirclePieView;
import com.sskj.linechat.widght.CircleScaleView;
import com.sskj.linechat.widght.LineView;
import com.sskj.linechat.widght.TimeRateView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {


    private LineView lineView;
    private List<LineBean> lineBeans = new ArrayList<>();
    //lineview设置绘制宽高
    private float layoutWidth = 1080;
    private float layoutHeight = 1920;
    //模拟期数
    private int qishu = 0;
    //圆环
    private CircleScaleView circleScaleView;
    //根据数值上下滑动的滑块
    private TimeRateView timeRateView;

    //方块可移动的高度
    private float timeRateViewHeight = UIUtil.dp2px(74);

    private CirclePieView circlePieView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getData();
    }

    @SuppressLint("CheckResult")
    private void initView() {
        circleScaleView = findViewById(R.id.circleScaleView);
        timeRateView = findViewById(R.id.timeRateView);
        circlePieView = findViewById(R.id.circlePieView);

        if (circleScaleView != null) {
            circleScaleView.setCirclePercent(1, 2, 2);
        }
        if (circlePieView != null) {
            ArrayList<CirclePieView.ViewData> viewDatas = new ArrayList<>();
            viewDatas.add(new CirclePieView.ViewData(1, "1"));
            viewDatas.add(new CirclePieView.ViewData(2, "2"));
            viewDatas.add(new CirclePieView.ViewData(3, "3"));
            circlePieView.setData(viewDatas);
        }
        layoutWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth() / 2;
        lineView = findViewById(R.id.lineView);

        ClickUtil.click(200, findViewById(R.id.add), () -> {
            if (lineBeans.size() >= 100) {
                lineBeans.remove(lineBeans.size() - 1);
            }
            qishu++;
            LineBean lineBean = new LineBean();
            lineBean.setType(qishu % 3);
            lineBean.setPrice(qishu + 1 + "");
            lineBean.setHoldIsLeft(qishu % 4 == 0);
            lineBean.setCycleNum(qishu + "");
            lineBeans.add(0, lineBean);

            lineView.getPoints(lineBeans, layoutWidth, layoutHeight);
        });
        //设置初始值
        timeRateView.getPoint(0, 0, timeRateViewHeight);
        //随机改变
        ClickUtil.click(200, findViewById(R.id.randomBtn), () -> {
            Random random = new Random();
            float v = random.nextFloat() * 50;
            if (v > 20) {
                v = v - 40;
            }
            v = new BigDecimal(NumberUtil.keep(v + "", 2, false)).floatValue();
            timeRateView.getPoint(v, 0, timeRateViewHeight);
        });

        ClickUtil.click(findViewById(R.id.goSign), () -> {
            new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(aBoolean -> {
                        if (aBoolean) {
                            SignPlatformActivity.start(this);
                        } else {
                            ToastUtil.showShort("授权失败");
                        }
                    });
        });
    }


    private void getData() {

        for (int i = 0; i < 100; i++) {
            LineBean lineBean = new LineBean();
            lineBean.setType(i % 3);
            lineBean.setPrice(i + 1 + "");
            lineBean.setHoldIsLeft(i % 4 == 0);
            lineBean.setCycleNum(i + "");
            lineBeans.add(lineBean);
        }

        lineView.getPoints(lineBeans, layoutWidth, layoutHeight);
    }
}
